﻿
function subentity(instance) {
    var self = this;
    var isNewFrom = fs.form.mode == "New";

    instance.description = instance.description.replace("&amp;", "&");
    self.accountcode = instance.accountcode;
    self.description = instance.description;
    self.unit = instance.unit;

    self.entity = ko.observable(instance.entity);
    self.RGCDescription = ko.observable(instance.RGCDescription);
    self.RGCDescription.subscribe(function (newValue) {
        var count = 0;
        viewModel.columns.forEach(function (obj) {
            if (obj.reference) {
                self.subrow = true;
                obj.expression(self);
            }
        });

        instance.parent.subrow().forEach(function (s) {
            if (unWarpValue(s.RGCDescription) == newValue) {
                count++;
                if (count > 1) {
                    alert(newValue + "already exist!");
                    return;
                }
            }
        });
    });

    viewModel.columns.forEach(function (obj) {
        var skip = instance.subskip && instance.subskip.indexOf(obj.rowText.split('_')[1]) != -1;

        if (!skip) {
            var value = isNewFrom ? 0 : instance[obj.rowText];
            self[obj.rowText] = ko.observable(value ? value : 0.00);
        }

    })

    return self;
}


function Item(instance) {
    var self = this;

    self.headrow = self.emptyrow = self.datarow = self.controlrow = self.withsubrow = self.bold, self.insertcontrol = false;

    if (instance.description)
        instance.description = instance.description.replace("&amp;", "&");

    self.insertcontrol = instance.insertcontrol ? instance.insertcontrol : false;
    self.bold = instance.bold ? instance.bold : false;
    self.hide = instance.hide ? instance.hide : false;
    self.unit = instance.unit ? fs_unitMerger(instance.unit) : fs.form.entitycurrency;
    self.digits = instance.digits ? instance.digits : fs.sys.defaultDigits;
    self.type = instance.type ? instance.type : undefined;
     
    self.ddreport = instance.ddreport;
    self.BindingRecords = instance.BindingRecords;

    self.Log = instance.Log;
    if (instance.emptyrow) {
        self.emptyrow = instance.emptyrow;
        self.description = instance.description;
        self.accountcode = "";
        return self;
    }
    if (instance.headrow) {
        self.description = instance.description;
        self.headrow = instance.headrow;
        return self;
    }

    self.controlrow = instance.controlrow ? instance.controlrow : false;
    self.datarow = instance.datarow;
    //  self.Process = instance.Process;
    self.allowCopyDataFromLastMonth = instance.allowCopyDataFromLastMonth;
    self.dataFromLastMonth = instance.dataFromLastMonth;
    self.hasProcess = instance.hasProcess;
    self.YTDEqualCM = instance.YTDEqualCM;

    self.accountcode = instance.accountcode;
    self.relacc = instance.relacc;
    self.description = instance.description;
    self.defaultValue = instance.defaultValue;
    self.afterBinding = instance.afterBinding; 
    

    self.skip = instance.skip;
    self.index = instance.index;
    self.rgc = instance.rgc;
    self.subskip = instance.subskip;

    self.withsubrow = $.isArray(instance.subrow);

    self.style = ko.observable(instance.style);
    self.styleInit = function (col, row) {

        if (col.hide) return "fs_disable";
        if (col.style) return col.style;
        var style = row[col.rowText + 'style'];
        col.calcfield = style ? true : false
        if (style) return style;

        col.readonly = style ? true : false;
        if (style == "green_loading")
            col.readonly = false;

        return style;
    }

    self.manual = function (col, row) {
        if (typeof row[col.rowText] == 'undefined' || fs.form.mode == 'Display' || col['readonly'] || !col['edit'] || row['readonly'])
            return false;

//        var cp = instance.Process.filter(function (s) { return s.Field == col.rowText });
//        if (cp.length > 0 && cp[0].manual)
//            return true;
        var style = self.styleInit(col, row);
        return style != "blue" && style != "green";
    }

    if (self.withsubrow) {

        self.subrow = ko.observableArray([]);
        instance.subrow.forEach(function (obj) {
            obj.parent = self;
            self.subrow.push(new subentity(obj));
        });

        self.addItem = function () {
            self.subrow.push(new subentity({ parent: instance.parent, accountcode: instance.accountcode, description: instance.description, RGCDescription: instance.RGCDescription, subskip: instance.subskip }));
        }
        self.removeItem = function (item) {
            self.subrow.remove(item);
        }
        self.style("blue");
    }
    fieldLoader(self, instance);

    instance.parent = self;

    return self;
}

var fieldLoader = function (self, instance) {

    ko.utils.arrayForEach(viewModel.columns, function (obj) {
        var skip = instance.skip && instance.skip.indexOf(obj.rowText.split('_')[1]) != -1;
        if (!skip) {
            var isNewFrom = fs.form.mode.toUpperCase() == "NEW";
            var isDisplayPage = fs.form.mode.toUpperCase() == "DISPLAY";
            var isControl = self.accountcode.toUpperCase() == "CONTROL";
            self[obj.rowText] = ko.observable(0);

            if (instance.Process) {
                var process = instance.Process.filter(function (o) { return o.Field == obj.rowText || o.Field == "All"; });
                if (process.length > 0) {
                    process = process[0]

                    if (process.Exp.mode == "refrence")
                        self[obj.rowText + "style"] = "green";
                    else
                        self[obj.rowText + "style"] = "blue";
                    if (process.Exp.style)
                        self[obj.rowText + "style"] = process.Exp.style;
                }
            } //Change 2014-06-07

            if (self.withsubrow) {
                self[obj.rowText] = ko.computed(function () {
                    var total = 0;
                    ko.utils.arrayForEach(self.subrow(), function (item) {
                        var value = parseFloat(unWarpValue(item[obj.rowText]));
                        if (!isNaN(value)) {
                            total += value;
                        }
                    });
                    return total.toFix();
                }, self);
                //  self.style("blue");
                self[obj.rowText + "style"] = "blue";
            }
            else {
                if (isDisplayPage) { //Change 2014-06-07  
                    if (isControl)
                        expressionConnector(self, obj, instance, isControl);
                    else {
                        simpleConnector(self, obj, instance);
                    }
                }
                else if (instance.Process) { //Change 2014-06-07
                    expressionConnector(self, obj, instance, isControl);
                    self.style("");
                }
                else if (obj.reference) {
                    obj.expression(self);
                }
                else {
                    simpleConnector(self, obj, instance);
                }
            }
        }
    });

}

var firstColumns = [
  { lastMonthField: "L_Dec", index: 0, headerText: "Reforecast", edit: true, width: 80, rowText: "Reforecast" },
  { lastMonthField: "L_Dec", index: 1, headerText: "Jan", edit: true, width: 80, rowText: "Jan" },
  { lastMonthField: "Jan", index: 2, headerText: "Feb", edit: true, width: 80, rowText: "Feb" },
  { lastMonthField: "Feb", index: 3, headerText: "Mar", edit: true, width: 80, rowText: "Mar" },
  { lastMonthField: "Mar", index: 4, headerText: "Apr", edit: true, width: 80, rowText: "Apr" },
  { lastMonthField: "Apr", index: 5, headerText: "May", edit: true, width: 80, rowText: "May" },
  { lastMonthField: "May", index: 6, headerText: "Jun", edit: true, width: 80, rowText: "Jun" },
  { lastMonthField: "Jun", index: 7, headerText: "Jul", edit: true, width: 80, rowText: "Jul" },
  { lastMonthField: "Jul", index: 8, headerText: "Aug", edit: true, width: 80, rowText: "Aug" },
  { lastMonthField: "Aug", index: 9, headerText: "Sep", edit: true, width: 80, rowText: "Sep" },
  { lastMonthField: "Sep", index: 10, headerText: "Oct", edit: true, width: 80, rowText: "Oct" },
  { lastMonthField: "Oct", index: 11, headerText: "Nov", edit: true, width: 80, rowText: "Nov" },
  { lastMonthField: "Nov", index: 12, headerText: "Dec", edit: true, width: 80, rowText: "Dec" }
];
